package com.getpebble.android.bluetooth.protocol;

import java.nio.ByteBuffer;

public class ProtocolMessage {
    private static final String TAG = ProtocolMessage.class.getSimpleName();
    private byte[] mData;
    private short mEndpointId = (short) -1;

    public ProtocolMessage(short endpointId, byte[] data) {
        this.mEndpointId = endpointId;
        this.mData = data;
    }

    public static ProtocolMessage from(ByteBuffer buffer) {
        short size = buffer.getShort();
        short endpoint = buffer.getShort();
        if (size != buffer.remaining()) {
            throw new IllegalArgumentException("Invalid size for message, expected: " + size + ", got: " + buffer.remaining());
        }
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes, 0, bytes.length);
        return new ProtocolMessage(endpoint, bytes);
    }

    public short getEndpointId() {
        return this.mEndpointId;
    }

    public ByteBuffer getDataBuffer() {
        return ByteBuffer.wrap(this.mData).asReadOnlyBuffer();
    }

    public int getDataSize() {
        return this.mData.length;
    }
}
